package androidx.appcompat.view.menu;

import android.content.Context;
import android.content.res.Resources;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import androidx.appcompat.view.menu.m;
import androidx.appcompat.widget.l0;
import b.a.d;
import b.a.g;
import b.f.l.s;

final class q extends k implements PopupWindow.OnDismissListener, AdapterView.OnItemClickListener, m, View.OnKeyListener {
    private static final int w = g.abc_popup_menu_item_layout;

    /* renamed from: c  reason: collision with root package name */
    private final Context f236c;

    /* renamed from: d  reason: collision with root package name */
    private final g f237d;
    private final f e;
    private final boolean f;
    private final int g;
    private final int h;
    private final int i;
    final l0 j;
    final ViewTreeObserver.OnGlobalLayoutListener k = new a();
    private final View.OnAttachStateChangeListener l = new b();
    private PopupWindow.OnDismissListener m;
    private View n;
    View o;
    private m.a p;
    ViewTreeObserver q;
    private boolean r;
    private boolean s;
    private int t;
    private int u = 0;
    private boolean v;

    class a implements ViewTreeObserver.OnGlobalLayoutListener {
        a() {
        }

        public void onGlobalLayout() {
            if (q.this.b() && !q.this.j.x()) {
                View view = q.this.o;
                if (view == null || !view.isShown()) {
                    q.this.dismiss();
                } else {
                    q.this.j.g();
                }
            }
        }
    }

    class b implements View.OnAttachStateChangeListener {
        b() {
        }

        public void onViewAttachedToWindow(View view) {
        }

        public void onViewDetachedFromWindow(View view) {
            ViewTreeObserver viewTreeObserver = q.this.q;
            if (viewTreeObserver != null) {
                if (!viewTreeObserver.isAlive()) {
                    q.this.q = view.getViewTreeObserver();
                }
                q qVar = q.this;
                qVar.q.removeGlobalOnLayoutListener(qVar.k);
            }
            view.removeOnAttachStateChangeListener(this);
        }
    }

    public q(Context context, g gVar, View view, int i2, int i3, boolean z) {
        this.f236c = context;
        this.f237d = gVar;
        this.f = z;
        this.e = new f(gVar, LayoutInflater.from(context), this.f, w);
        this.h = i2;
        this.i = i3;
        Resources resources = context.getResources();
        this.g = Math.max(resources.getDisplayMetrics().widthPixels / 2, resources.getDimensionPixelSize(d.abc_config_prefDialogWidth));
        this.n = view;
        this.j = new l0(this.f236c, (AttributeSet) null, this.h, this.i);
        gVar.c(this, context);
    }

    private boolean z() {
        View view;
        if (b()) {
            return true;
        }
        if (this.r || (view = this.n) == null) {
            return false;
        }
        this.o = view;
        this.j.G(this);
        this.j.H(this);
        this.j.F(true);
        View view2 = this.o;
        boolean z = this.q == null;
        ViewTreeObserver viewTreeObserver = view2.getViewTreeObserver();
        this.q = viewTreeObserver;
        if (z) {
            viewTreeObserver.addOnGlobalLayoutListener(this.k);
        }
        view2.addOnAttachStateChangeListener(this.l);
        this.j.z(view2);
        this.j.C(this.u);
        if (!this.s) {
            this.t = k.o(this.e, (ViewGroup) null, this.f236c, this.g);
            this.s = true;
        }
        this.j.B(this.t);
        this.j.E(2);
        this.j.D(n());
        this.j.g();
        ListView l2 = this.j.l();
        l2.setOnKeyListener(this);
        if (this.v && this.f237d.x() != null) {
            FrameLayout frameLayout = (FrameLayout) LayoutInflater.from(this.f236c).inflate(g.abc_popup_menu_header_item_layout, l2, false);
            TextView textView = (TextView) frameLayout.findViewById(16908310);
            if (textView != null) {
                textView.setText(this.f237d.x());
            }
            frameLayout.setEnabled(false);
            l2.addHeaderView(frameLayout, (Object) null, false);
        }
        this.j.o(this.e);
        this.j.g();
        return true;
    }

    public void a(g gVar, boolean z) {
        if (gVar == this.f237d) {
            dismiss();
            m.a aVar = this.p;
            if (aVar != null) {
                aVar.a(gVar, z);
            }
        }
    }

    public boolean b() {
        return !this.r && this.j.b();
    }

    public boolean c() {
        return false;
    }

    public void dismiss() {
        if (b()) {
            this.j.dismiss();
        }
    }

    public void g() {
        if (!z()) {
            throw new IllegalStateException("StandardMenuPopup cannot be used without an anchor");
        }
    }

    public void h(m.a aVar) {
        this.p = aVar;
    }

    public boolean i(r rVar) {
        if (rVar.hasVisibleItems()) {
            l lVar = new l(this.f236c, rVar, this.o, this.f, this.h, this.i);
            lVar.j(this.p);
            lVar.g(k.x(rVar));
            lVar.i(this.m);
            this.m = null;
            this.f237d.e(false);
            int e2 = this.j.e();
            int h2 = this.j.h();
            if ((Gravity.getAbsoluteGravity(this.u, s.x(this.n)) & 7) == 5) {
                e2 += this.n.getWidth();
            }
            if (lVar.n(e2, h2)) {
                m.a aVar = this.p;
                if (aVar == null) {
                    return true;
                }
                aVar.b(rVar);
                return true;
            }
        }
        return false;
    }

    public void j(boolean z) {
        this.s = false;
        f fVar = this.e;
        if (fVar != null) {
            fVar.notifyDataSetChanged();
        }
    }

    public void k(g gVar) {
    }

    public ListView l() {
        return this.j.l();
    }

    public void onDismiss() {
        this.r = true;
        this.f237d.close();
        ViewTreeObserver viewTreeObserver = this.q;
        if (viewTreeObserver != null) {
            if (!viewTreeObserver.isAlive()) {
                this.q = this.o.getViewTreeObserver();
            }
            this.q.removeGlobalOnLayoutListener(this.k);
            this.q = null;
        }
        this.o.removeOnAttachStateChangeListener(this.l);
        PopupWindow.OnDismissListener onDismissListener = this.m;
        if (onDismissListener != null) {
            onDismissListener.onDismiss();
        }
    }

    public boolean onKey(View view, int i2, KeyEvent keyEvent) {
        if (keyEvent.getAction() != 1 || i2 != 82) {
            return false;
        }
        dismiss();
        return true;
    }

    public void p(View view) {
        this.n = view;
    }

    public void r(boolean z) {
        this.e.d(z);
    }

    public void s(int i2) {
        this.u = i2;
    }

    public void t(int i2) {
        this.j.d(i2);
    }

    public void u(PopupWindow.OnDismissListener onDismissListener) {
        this.m = onDismissListener;
    }

    public void v(boolean z) {
        this.v = z;
    }

    public void w(int i2) {
        this.j.n(i2);
    }
}
